package top.guinguo.analyzer_03.util;

import org.apache.lucene.analysis.*;
import org.apache.lucene.util.Version;

import java.io.Reader;
import java.util.Set;

/**
 * Created by guin_guo on 2016/12/18.
 */
public final class MyStopAnalyzer extends Analyzer {
    private Set stops;

    public MyStopAnalyzer(String[] myStops, boolean addOriginal) {
        //会自动将字符串数组转换为Set
        this.stops = StopFilter.makeStopSet(Version.LUCENE_35, myStops, true);
        //是否将原有的停用词加入到现在的停用词
        if (addOriginal) {
            this.stops.addAll(StopAnalyzer.ENGLISH_STOP_WORDS_SET);
        }
    }

    public MyStopAnalyzer() {
        //获取原有的停用词
        this.stops = StopAnalyzer.ENGLISH_STOP_WORDS_SET;
    }

    public TokenStream tokenStream(String fieldName, Reader reader) {
        //为这个分词器设定过滤链和Tokenizer，一个是小写过滤器，一个是停词过滤器
        return new StopFilter(Version.LUCENE_35,
                new LowerCaseFilter(Version.LUCENE_35,
                        new LetterTokenizer(Version.LUCENE_35, reader)), stops);
    }
}
